//
// Created by hjie on 23-6-1.
//
#pragma once

#include "channel.h"
#include "udp_socket.h"
#include "define.h"
#include "data_buffer.h"
#include <cstdint>
#include <memory>
#include <string>
#include <sys/socket.h>
#include <unistd.h>

class LoopEvent;

class UdpClient
{
public:
    UdpClient(LoopEvent * loop, const std::string & ip, uint16_t port);
    ~UdpClient();
    void StartClient();
    void SetOnMessage(OnUdpPacketMessage && message_func);
    void DoReadMsg(int64_t channel_id);
    int DoSendMsg(const uint8_t * data, int length);
    void DoAsyncMsg(const uint8_t * data, int length);
    void DoSendMsg(struct sockaddr * addr, socklen_t addr_length, const uint8_t * data, int length);
private:
    LoopEvent * m_loop_event;
    UdpSocket m_udp_socket;
    OnUdpPacketMessage m_message_func;
    std::shared_ptr<Channel> m_channel;
private:
    DataBuffer * m_data_buffer;
    struct sockaddr_in m_remote_addr{};
    socklen_t m_remote_len;
};
